package com.example.testlibraryproject.adapters;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Message;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.example.testlibraryproject.R;
import com.example.testlibraryproject.utils.L;

public class MessagesListAdapter extends BaseAdapter {

	ArrayList <Message> messagesItems;
	Context context;
	//OnMessageDismissBtnClickListener mOnMessageDismissBtnClickListener;
	static class ViewHolder{
		public TextView description;
		public Button dismissMsgBtn;
		public Button gotoGuideBtn;
	}

	public MessagesListAdapter(Context context, ArrayList<Message> Messages) {
		this.context = context;
		this.messagesItems = Messages;
		L.v("size of CustomListAdapter:"+Messages.size());
	}
	
	public MessagesListAdapter(Context context) {
		this.context = context;
	
	}
	
	
	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public ArrayList<Message> getMessagesItems() {
		return messagesItems;
	}

	public void setMessagesItems(ArrayList<Message> messagesItems) {
		this.messagesItems = messagesItems;
	}
	
	/**
	 * Funtion to add new message items to it
	 * @param messagesItems
	 */
	public void addMessagesItems(ArrayList<Message> messagesItems) {
		this.messagesItems.addAll(messagesItems);
	}


	@Override
	public int getCount() {
		return messagesItems.size();
	}

	@Override
	public Object getItem(int index) {
		return messagesItems.get(index);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}
	/**
	 * Listener for message dismiss item click
	 *
	 */
	/*public interface OnMessageDismissBtnClickListener {
		public abstract void OnMessageDismissBtnClick(int pos);
	}
	public void OnMessageDismissBtnClickListener(OnMessageDismissBtnClickListener listener) {
		mOnMessageDismissBtnClickListener = listener;
	}*/
	
	@Override
	public View getView(int position, final View convertView,final ViewGroup parent) {
		L.v("inside getView for postion"+position);
		//final View rowView = convertView;
		/*if(rowView == null){
			LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			rowView = inflater.inflate(R.layout.message_row, null);

			ViewHolder viewHolder = new ViewHolder();
			//viewHolder.number = (TextView) rowView.findViewById(R.id.numberView);
			viewHolder.description = (TextView) rowView.findViewById(R.id.messages_description);
			viewHolder.dismissMsgBtn = (Button) rowView.findViewById(R.id.message_dismiss_btn);
			viewHolder.gotoGuideBtn = (Button) rowView.findViewById(R.id.message_goto_guide_btn);
			//so as speedify the process of list view scrolling
			rowView.setTag(viewHolder);

		}*/
		L.v("item checked position "+position+":"+((ListView)parent).isItemChecked(position));
		//to check if item is checked
		boolean itemChecked = ((ListView)parent).isItemChecked(position);

		//_dn

		LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		final View rowView  = inflater.inflate(R.layout.message_row, null);

		//ViewHolder viewHolder = new ViewHolder();
		//viewHolder.number = (TextView) rowView.findViewById(R.id.numberView);


		final int currentMessagePos = position;
		final TextView description = (TextView) rowView.findViewById(R.id.messages_description);

		final Button dismissMsgBtn = (Button) rowView.findViewById(R.id.message_dismiss_btn);
		final Button gotoGuideBtn = (Button) rowView.findViewById(R.id.message_goto_guide_btn);

		
		final LinearLayout msgBtnsLayout = (LinearLayout) rowView.findViewById(R.id.message_btns_layout);
		dismissMsgBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				//L.v("dimiss btn clicked for pos:"+position);
				messagesItems.remove(currentMessagePos);
				notifyDataSetInvalidated();
				
				//((ListView)parent).setItemChecked(currentMessagePos, false);
				//parent.removeViewAt(position);
				//notifyDataSetChanged();
				//rowView.setVisibility(View.GONE);
				

			}
		});
		gotoGuideBtn.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				L.v("gotoGuide btn clicked for pos:"+currentMessagePos);
			}
		});

		//_dn
		//final ViewHolder holder = (ViewHolder) rowView.getTag();



		
		//String text = messagesItems.get(position).getText().trim();
		String text ="your text goes here";
		L.v("This line text is::"+text);
		String strToReplace = "<br/>";
		
		if(text.startsWith(strToReplace )){
			//log("inside begins with");
			text = text.substring(strToReplace.length());
			
		}
		L.v("Converted text is::"+text);
		description.setText(Html.fromHtml(text));
		
		rowView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				L.v("Message view clicked");
				
				/*dismissMsgBtn.setVisibility(View.VISIBLE);
				gotoGuideBtn.setVisibility(View.VISIBLE);*/
				msgBtnsLayout.setVisibility(View.VISIBLE);
				//setting the value to max of lines that can be shown
				description.setMaxLines(Integer.MAX_VALUE);
				description.setTypeface(Typeface.DEFAULT_BOLD);
				rowView.setBackgroundColor(0x00ff00);

			}
		});

		return rowView;
	}

}
